using System;
using System.Collections.Generic;
using System.Text;

namespace HubLog
{
    public class LogRecord
    {
        //
		private bool _New;
		private string _Instance;
		private DateTime _TimeStamp;
		private string _Source;
        private Int32 _Pid;
		private string _Severity;
		private Int32 _ErrorNo;
        private string _Message;
        private Int32 _HostNo;
        private FlagValues _Flag;
        //
        public enum FlagValues { Normal = 0, Marked = 1, InternalConnected=-1, InternalDisconnected=-2 };
        //
        public LogRecord()
        {
            this.Clear();
        }
        //
        public void Clear()
        {
            _New = true;
            _Instance = string.Empty;
            _TimeStamp = DateTime.Now;
            _Source = string.Empty;
            _Pid = 0;
            _Severity = string.Empty;
            _ErrorNo = 0;
            _Message = string.Empty;
            _HostNo = 0;
            _Flag = 0;
        }
        //
        public bool New{get { return this._New; }}
        public string Instance { get { return this._Instance; } }
        public DateTime TimeStamp { get { return this._TimeStamp; } }
        public string Source { get { return this._Source; } }
        public Int32 Pid { get { return this._Pid; } }
        public string Severity { get { return this._Severity; } }
        public Int32 ErrorNo { get { return this._ErrorNo; } }
        public string Message { get { return this._Message; } }
        public Int32 HostNo { get { return this._HostNo; } }
        public FlagValues Flag { set { _Flag = value; } get { return this._Flag; } }

        //
        public string SetInstance(string instance)
        {
            return this._Instance = instance;
        }
        //public bool ParseLine(string Instance, string line)
        public bool ParseLine(int HostNo, string line)
        {
            int n;
            string[] lt = line.Split('|');
            //
            this.Clear();
            //this._Instance = Instance;
            this._HostNo = HostNo;
            //
            if (4 != lt.GetLength(0))
            {
                this._TimeStamp = DateTime.Now;
                this._Message = line.Trim();
                return false;
            }
            //
            lt[0].Replace(@"/", @"-");
            try
            {
                this._TimeStamp = DateTime.Parse(lt[0].Trim());
            }
            catch
            {
                this._TimeStamp = DateTime.Now;
            }
            this._Source = lt[1].Trim();
            if (-1 != (n = this._Source.IndexOf("[")))
            {
                if (n > 0)
                {
                    this._Pid = StringToInt(this._Source.Substring(n + 1));
                    this._Source = this._Source.Substring(0, n);
                }
            }
            this._Severity = lt[2].Trim();
            this._Message = lt[3].Trim();
            if ('#' == this._Message[0])
            {
                this._ErrorNo = StringToInt(this._Message.Substring(1));
            }
            return true;
        }
        private static int StringToInt(string s)
        {
            int i, n, m;
            string d;
            char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            if (-1 == (n = s.IndexOfAny(digits))) { return -1; }
            d = string.Empty;
            for (m = n; m < s.Length; m++)
            {
                if (s[m] >= '0' && s[m] <= '9')
                {
                    d += s[m];
                }
                else break;
            }
            try
            {
                i = int.Parse(d);
            }
            catch
            {
                i = 0;
            }
            return i;
        }
        //
        //public void CreateConnectionRecord(string Instance)
        public void CreateConnectionRecord(int HostNo, TelnetCat tc)
        {
            this.Clear();
            //this._Instance = Instance;
            this._HostNo = HostNo;
            this._Source = "Internal";
            this._Message = string.Format("Connected --> {0}:{1}",tc.ServerName,tc.ServerPort);
            this._Flag = LogRecord.FlagValues.InternalConnected;
        }
        //
        //public void CreateDisconnectionRecord(string Instance)
        public void CreateDisconnectionRecord(int HostNo, TelnetCat tc)
        {
            this.Clear();
            //this._Instance = Instance;
            this._HostNo = HostNo;
            this._Source = "Internal";
            this._Message = string.Format("Disconnected --> {0}:{1}", tc.ServerName, tc.ServerPort);
            this._Flag = LogRecord.FlagValues.InternalDisconnected;
        }
        //
        //
        //public void CreateRandomRecord(string Instance, Int32 HostNo)//test
        public void CreateRandomRecord(string Instance, Int32 HostNo)//test
        {
            Random r = new Random();
            //
            int rInt = r.Next(0, 999);
            //
            this.Clear();
            this._Instance = Instance;
            this._Source = "Random"+rInt.ToString();
            this._Pid = rInt;
            this._Severity = "Severity" + rInt.ToString();
            this._ErrorNo = rInt;
            this._Message = "random" + rInt.ToString();
            this._HostNo = HostNo;
            this._Flag = 0;
        }
        //
        public override string ToString()
        {
            return this._Instance + "," + this._TimeStamp.ToString() + "," + this._Source + "," + this._Severity + "," + this._ErrorNo.ToString() + "," + this._Message+","+this._HostNo.ToString()+","+this._Flag.ToString()+".";
        }
        //
    }//class
}//namespace
